<template>
  <div class="jeecg-basic-table jeecg-basic-table-form-container">
    <!--查询区域-->
    <div class="">
      <a-form ref="formRef" class="jeecg-basic-table-form" @keyup.enter.native="reload" :model="queryParam"
        :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="6">
            <a-form-item label="委托单位" name="entName">
              <!-- <j-dict-select-tag placeholder="" v-model:value="queryParam.entName" :show-choose-option="false"
                dictCode="lab_enterprise,ent_name,ent_name" /> -->
                <j-input placeholder="" v-model:value="queryParam.entName"></j-input>
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item label="经办人" name="handler">
              <j-input placeholder="" v-model:value="queryParam.handler"></j-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :lg="6">
              <a-form-item label="样品大类" name="sampleType">
                <j-dict-select-tag placeholder="" v-model:value="queryParam.sampleType" :show-choose-option="false"
                  dictCode="lab_items,name,id,type='A'" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="reload">查询</a-button>
                <a-button preIcon="ant-design:reload-outlined" @click="searchReset" style="margin-left: 8px">重置</a-button>
                <a @click="toggleSearchStatus = !toggleSearchStatus" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <Icon :icon="toggleSearchStatus ? 'ant-design:up-outlined' : 'ant-design:down-outlined'" />
                </a>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :expandedRowKeys="expandedRowKeys" @expand="handleExpand">
      <!-- 内嵌table区域 begin -->
      <template #expandedRowRender="{ record }">
        <a-tabs tabPosition="top">
          <a-tab-pane tab="实验项目" key="labItems" forceRender>
            <LabItemsSubTable :id="expandedRowKeys[0]" />
          </a-tab-pane>
        </a-tabs>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small"
          @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 实验报告 -->
    <TableReportModal @register="registerReportModal" @success="handleSuccess"></TableReportModal>
    <!-- 实验记录 -->
    <TableModal @register="registerItemsModal"></TableModal>
  </div>
</template>

<script lang="ts" name="items-report"  setup>
import { ref, reactive } from 'vue';
import { JInput,JDictSelectTag } from '/@/components/Form';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { useModal } from '/@/components/Modal';
import { columns } from './LabItemResultList.data';
import { list } from './LabItemResultList.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import LabItemsSubTable from './subTables/LabItemsSubTable.vue';
import TableModal from './LabItemResultFrom.vue';
import TableReportModal from './LabItemReportFrom.vue';



const formRef = ref();
// 展开key
const expandedRowKeys = ref<any[]>([]);
const queryParam = reactive<any>({
  auditMark: '复审通过',
});
//注册model
const [registerItemsModal, { openModal }] = useModal();
//注册model
const [registerReportModal, { openModal:openRModal }] = useModal();

//注册table数据
const { tableContext } = useListPage({
  tableProps: {
    api: list,
    columns: columns,
    canResize: false,
    useSearchForm: false,
    showIndexColumn: true,
    showTableSetting: false,
    actionColumn: {
      width: 120,
      fixed: 'right'
    },
    beforeFetch: (params) => {
      return Object.assign(params, queryParam);
    },
  },
})

const [registerTable, { reload }, { selectedRowKeys }] = tableContext

/**
  * 展开事件
  * */
function handleExpand(expanded, record) {
  expandedRowKeys.value = [];
  if (expanded === true) {
    expandedRowKeys.value.push(record.id)
  }
}




function handleRecord(record) {
  openModal(true, {
    record,
  });
}

function handleReport(record) {
  openRModal(true, {
    record,
    disabled:true,
  });
}
function handleDetail(record) {
  openRModal(true, {
    record,
    disabled:false,
  });
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}


function getTableAction(record) {
  if(record.reportCode != null && record.reportCode != ""){
    return [
      {
        label: '实验报告',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '实验记录',
        onClick: handleRecord.bind(null, record),
      },
    ]
  }else{
    return [
      {
        label: '新增报告',
        onClick: handleReport.bind(null, record),
      },
      {
        label: '实验记录',
        onClick: handleRecord.bind(null, record),
      },
    ]
  }
  

}


/* ----------------------以下为原生查询需要添加的-------------------------- */
const toggleSearchStatus = ref<boolean>(false);
const labelCol = reactive({
  xs: { span: 24 },
  sm: { span: 7 },
});
const wrapperCol = reactive({
  xs: { span: 24 },
  sm: { span: 16 },
});
/**
 * 重置
 */
function searchReset() {
  formRef.value.resetFields();
  selectedRowKeys.value = [];
  //刷新数据
  reload();
}

</script>
<style lang="less" scoped>
.jeecg-basic-table-form-container {
  .table-page-search-submitButtons {
    display: block;
    margin-bottom: 24px;
    white-space: nowrap;
  }

  .query-group-cust {
    width: calc(50% - 15px);
    min-width: 100px !important;
  }

  .query-group-split-cust {
    width: 30px;
    display: inline-block;
    text-align: center
  }
}

// .jeecg-basic-table-form {
//   height: 58px;
// }

:deep(.ant-table-title) {
  display: none;
}
</style>
